#import "SMTimeAgo.h"

@implementation SMTimeAgo

+ (NSString *)stringFromDate:(NSDate *)date {
    if (!date) return @"刚刚";
    NSTimeInterval s = [[NSDate date] timeIntervalSinceDate:date];
    if (s < 60) return @"刚刚";
    NSInteger m = s / 60;
    if (m < 60) return [NSString stringWithFormat:@"%ld 分钟前", (long)m];
    NSInteger h = m / 60;
    if (h < 24) return [NSString stringWithFormat:@"%ld 小时前", (long)h];
    NSInteger d = h / 24;
    if (d == 1) return @"一天前";
    if (d == 2) return @"2 天前";
    if (d < 7)  return [NSString stringWithFormat:@"%ld 天前", (long)d];
    NSInteger w = d / 7;
    if (w == 1) return @"一周前";
    if (w == 2) return @"2 周前";
    if (w == 3) return @"3 周前";
    return @"一个月前";
}

+ (NSDate *)dateFromUnix:(NSTimeInterval)ts {
    // 兼容毫秒级时间戳（大于 10^12）
    if (ts > 1000000000000) {
        ts /= 1000.0;
    }
    return [NSDate dateWithTimeIntervalSince1970:ts];
}

@end
